<?php

namespace Home\Controller;

use Think\Controller;
use Think\Verify;
use Think\Model;

class IndexController extends Controller
{

    private function topData()
    {
        $rp = M('RyProduct');
        $data = $rp->order('ry_product.id desc')->where('ry_product.class_id=1')->join('left join ry_img as ri on ry_product.id=ri.product_id')->select();
        $data2 = $rp->order('ry_product.id desc')->where('ry_product.class_id=2')->join('left join ry_img as ri on ry_product.id=ri.product_id')->select();
        $data3 = $rp->order('ry_product.id desc')->where('ry_product.class_id=3')->join('left join ry_img as ri on ry_product.id=ri.product_id')->select();
        $data4 = $rp->order('ry_product.id desc')->where('ry_product.class_id=4')->join('left join ry_img as ri on ry_product.id=ri.product_id')->select();
        $data5 = $rp->order('ry_product.id desc')->where('ry_product.class_id=5')->join('left join ry_img as ri on ry_product.id=ri.product_id')->select();
        $this->assign('ryjp', $data);
        $this->assign('ryjp2', $data2);
        $this->assign('ryjp3', $data3);
        $this->assign('ryjp4', $data4);
        $this->assign('ryjp5', $data5);


        //登录后
        $u = M('RyUser');

        $sid = $_SESSION['user_id'];
        if ($sid) {
            $userData = $u->where("ry_user.id=$sid")->join("inner join ry_address on ry_user.id=ry_address.user_id")->select();
            if ($userData) {
                $this->assign('UserData', $userData['0']);
            } else {
                $userData = $u->where("id=$sid")->select();
                $this->assign('UserData', $userData['0']);
            }
            $table = M('Ry_car');
            $data = $table->where("user_id=$sid")->select();
            foreach ($data as $row) {
                $array[] = $row['product_carnum'];
            }
            $a = array_sum($array);
            $this->assign("num", $a);

        }
    }

    public function ajax_lay()
    {
        $sid = $_SESSION['user_id'];
        if ($sid) {
            $table = M('Ry_car');
            $data = $table->where("user_id=$sid")->select();
            foreach ($data as $row) {
                $array[] = $row['product_carnum'];
            }
            $a = array_sum($array);
            $this->assign("num", $a);
        }

    }


    public function index()
    {
        header("Content-type:text/html;charset=utf-8");
        $this->topData();
        $this->display();
    }

//    验证码开始
    public function captcha()
    {
        $captcha = new Verify();
        $captcha->entry();
    }

//    验证码结束


//    判断是否登录 开始

    protected function _initialize()
    {
        $logon_rights = ['shopping', 'Settlement', 'bay_pay', 'order', 'private', 'myAddress_save', 'myAddress', ''];
        if (in_array(ACTION_NAME, $logon_rights)) {
            if (!$_SESSION['user_id']) {
                $this->error('您还没登录呢,请先登录', U('login'));
            }
        }
    }

    // 判断是否登录 结束


//    注册开始


    public function register()
    {
        $this->display();
    }

    public function register_save()
    {
        if (IS_POST) {
            $U = M('RyUser');
            $value = $_POST['captcha'];
            $captcha = new Verify();
            $res = $captcha->check($value);
            if ($res) {
                $data = $U->create();
                if ($data) {
                    $c['username'] = $data['username'];
                    if ($U->where($c)->count()) {
                        $this->error('用户名已存在');
                    }
                    $data['password'] = md5($data['password']);
                    $U->add($data);
                    $this->success('注册成功', U('login'));
                } else {
                    echo $U->getError();
                }
            } else {
                $this->error('验证码错误');
            }
            exit;
        }
    }

//    注册结束


//登录开始
    public function login_save()
    {
        if (IS_POST) {
            $U = M('RyUser');
            $value = $_POST['captcha'];
            $captcha = new Verify();
            $res = $captcha->check($value);
            if ($res) {
                $data = $U->create('', Model::MODEL_UPDATE);
                $data['password'] = md5($data['password']);
                $user = $U->where($data)->find();
                if ($user) {
                    session('user_id', $user['id']);
                    $this->success('登录成功', U('index'));
                } else {
                    $this->error('用户名或密码错误');
                }
            } else {
                $this->error('验证码错误');
            }
            exit;
        }
    }

//登录结束

//退出登录 开始

    public function loginOut()
    {
        session('user_id', null);
        $this->success('退出成功', U('index'));
    }

//退出登录 结束


//详情页 开始

    public function product()
    {
        $this->topData();
        $s_id = $_GET['id'];
        $rp = M('RyProduct');

        $data = $rp->where("ry_img.id=$s_id")->join("inner join ry_img on ry_img.product_id=ry_product.id")->select();
        $data = $data[0];
        $this->assign('data', $data);
        $this->display();

    }

    //详情页 结束

    public function shopping_del()
    {
        $this->topData();
        $data = M('RyCar');
        $product_id = $_GET['id'];
        var_dump($product_id);
        $data->startTrans();
        $ff = $data->delete($product_id);
        $data->commit();
        if ($ff) {
            $this->success('操作成功', '', 3);
        } else {
            $this->error('数据错误');
        }


    }


    public function shopping()
    {

        //商品表中有没有此商品

        //看购物车表中有没有这个商品id

        //没有就添加到购物车表中

        //有就神木都不做

        $this->topData();
        //那个用户的
        $userId = $_SESSION['user_id'];

        $m = M('RyCar');
        $data = $m->where("ry_car.user_id=$userId")->join("left join ry_product on ry_car.product_id=ry_product.id ")->select();
//        echo $m->getLastSql();
//       var_dump($data);

        $f = M('RyImg');
        $dd = $f->where("ry_car.user_id=$userId")->join('inner join ry_car on ry_car.product_id=ry_img.product_id')->select();
//        echo $f->getLastSql();
//        var_dump($dd);
        $newDd = [];
        foreach ($dd as $row) {
            $newDd[$row['product_id']] = $row;
        }
        // var_dump($newDd);

        foreach ($data as &$row) {
//            $row['picPath'] =
//            $row['product_id']
            $row['picInfo'] = $newDd[$row['product_id']];
            $row['image'] = $newDd[$row['product_id']]['image'];
        }
        //var_dump($data);
        $this->assign('data', $data);
        $this->display();

    }


    public function list_phone()
    {
        header('content-type:text/html;charset=utf-8');
        $this->topData();
        $table = M('RyProduct');
        $data = $table->where("ry_product.class_id=1")->join("inner join ry_img on ry_product.id=ry_img.product_id")->order('ry_product.id desc')->select();
        $this->assign('phone', $data);
        $this->display();
    }

    public function list_computer()
    {
        header('content-type:text/html;charset=utf-8');
        $table = M('RyProduct');
        $this->topData();
        $data = $table->where("ry_product.class_id=2")->join("inner join ry_img on ry_product.id=ry_img.product_id")->order('ry_product.id desc')->select();
        $this->assign('computer', $data);
        $this->display();
    }

    public function list_wear()
    {
        header('content-type:text/html;charset=utf-8');
        $table = M('RyProduct');
        $this->topData();
        $data = $table->where("ry_product.class_id=3")->join("inner join ry_img on ry_product.id=ry_img.product_id")->order('ry_product.id desc')->select();
        $this->assign('wear', $data);
        $this->display();
    }

    public function list_capacity()
    {
        header('content-type:text/html;charset=utf-8');
        $table = M('RyProduct');
        $this->topData();
        $data = $table->where("ry_product.class_id=4")->join("inner join ry_img on ry_product.id=ry_img.product_id")->order('ry_product.id desc')->select();
        $this->assign('capacity', $data);
        $this->display();
    }


    public function ajax()
    {
        $id = $_SESSION['user_id'];
        $product_carnum = $_POST['product_carnum'];
        $product_id = $_POST['product_id'];
        $data = M('ry_car');
        $ff = $data->create();

        $ff['user_id'] = $_SESSION['user_id'];
        $aa = $data->where("ry_car.user_id=$id and ry_car.product_id=$product_id")->select();
        $aa = $aa[0];
        $num = $aa['product_carnum'];
        $rr = $data->where("ry_car.user_id=$id and ry_car.product_id=$product_id")->count();
        if ($id) {
            if ($rr > 0) {

                $ff['product_carnum'] = $num + $product_carnum;
                $data->where("ry_car.user_id=$id and ry_car.product_id=$product_id")->save($ff);
            } else {
                $ff['product_carnum'] = $product_carnum;
                $data->add($ff);
            }
            $this->success('操作成功');
        } else {
            $this->error('输入错误');
        }
    }


    public function list_fit()
    {
        header('content-type:text/html;charset=utf-8');
        $table = M('RyProduct');
        $this->topData();
        $data = $table->where("ry_product.class_id=5")->join("inner join ry_img on ry_product.id=ry_img.product_id")->order('ry_product.id desc')->select();
        $this->assign('fit', $data);
        $this->display();
    }


    //立即下单

    //个人中心


    public function myAddress()
    {
        $this->topData();
        $this->display();
    }

    public function myAddress_save()
    {
        $RA = M('RyAddress');
        $myadd = $RA->create();
        if ($myadd) {
            $myadd['user_id'] = $_SESSION['user_id'];
            $RA->add($myadd);
            $this->success('添加地址成功', U('person'));
        } else {
            $this->error('添加地址失败', U('myAddress'));
        }
    }

    public function Settlement()
    {
//        $c = array();
//        $c['id']  = array('in',$p_id);
//        $goodsData = $table2->where($c)->select();
////        var_dump($table2->getLastSql());
//        var_dump($goodsData);
////        $p_num[2];
////        $p_num[4];
//        foreach ($goodsData as &$row){
//            $row['num'] = $p_num[$row['id']];
//            $row['priceAll'] = $row['num'] * $row['current_price'];
//        }
//        var_dump($goodsData);

        $this->topData();

        $u_id = $_SESSION['user_id'];
        if ($u_id) {
            $table2 = M('RyProduct');
            $table = M('ry_address');;
            $data = $table->where("ry_address.user_id=$u_id")->join("inner join ry_user on ry_user.id=ry_address.user_id")->select();
            $data = $data[0];
            $this->assign('list', $data);

            if ($_POST['product_id'] != "") {
                $product_id = i('post.product_id');
                $data2 = $table2->where("ry_product.id=$product_id")->join('inner join ry_img on ry_img.product_id=ry_product.id')->select();
                $this->assign('data', $data2);

            } else {
                $p_id = $_POST['chkGoogs'];
                $p_num = $_POST['goodsNum'];
                $arr = [];
                foreach ($p_id as $item) {
                    $data2 = $table2->where("ry_product.id=$item")->join('inner join ry_img on ry_img.product_id=ry_product.id')->select();
                    $arr[] = $data2[0];
                }
                foreach ($arr as &$row) {
                    $row['num'] = $p_num[$row['id']];
                    $row['allprice'] = $p_num[$row['id']] * $arr['current_price'];
                }
                $this->assign('data', $arr);
            }
            $this->display();
        } else {
            $this->error('你还没有登录', U('login'));
        }

    }


    public function order()
    {
        $this->topData();
        $this->display();
    }


    public function zfb()
    {

        $this->display();
    }

}

